<template>
	<!-- 商品列表 -->
	<view>
		<view v-if="tempArr.length" :style="[boxStyle]" :id="dataConfig.id">
			<view class="grid-title-box">
				<image src="/static/star.svg" mode=""></image>
				<view class="title-text">{{ dataConfig.titleConfig.val }}</view>
				<image src="/static/star.svg" mode=""></image>
			</view>
			<!-- 两列 -->
			<block v-if="itemStyle == 0">
				<view class="listC" :style="[gridGap]">
					<view class="item" :style="[contentBgColor]" v-for="(item, index) in tempArr" :key="index"
						@click="goDetail(item)">
						<view class="pictrue">
							<easy-loadimage :image-src="item.image" :radius="dataConfig.contentStyle.val">
							</easy-loadimage>
							<view v-if="item.activityStyle" :style="{ backgroundImage: `url(${item.activityStyle})` }"
								class="border-picture"></view>
						</view>
						<view class="text-info">
							<view class="title tn-text-ellipsis" :style="[titleColor]">
								<text v-if="item.productTags && item.productTags.locationLeftTitle.length"
									class="font-bg-red mr10 merType bg-color">{{item.productTags.locationLeftTitle[0].tagName}}</text>
								<span>{{ item.name }}</span>
							</view>
							<view class="acea-row row-middle price" :style="[priceColor]">
								<view>
									<text>￥</text>
									{{ item.price }}
								</view>
								<view class="car-icon-box">
									<image src="/static/car.svg" mode=""></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<!-- 三列 -->
			<block v-if="itemStyle == 1">
				<view class="listB" :style="[gridGap]">
					<view class="item" :style="[contentBgColor]" v-for="(item, index) in tempArr" :key="index"
						@click="goDetail(item)">
						<view class="pictrue" :style="[contentStyle]">
							<easy-loadimage :image-src="item.image" :radius="dataConfig.contentStyle.val">
							</easy-loadimage>
							<view v-if="item.activityStyle" :style="{ backgroundImage: `url(${item.activityStyle})` }"
								class="border-picture"></view>
						</view>
						<view class="text-info">
							<view class="title tn-text-ellipsis" :style="[titleColor]">
								<text v-if="item.productTags && item.productTags.locationLeftTitle.length"
									class="font-bg-red mr10 merType bg-color">{{item.productTags.locationLeftTitle[0].tagName}}</text>
								<span>{{ item.name }}</span>
							</view>
							<view class="price" :style="[priceColor]">
								<view>
									<text>￥</text>
									{{ item.price }}
								</view>
								<view class="car-icon-box">
									<image src="/static/car.svg" mode=""></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<view class='loadingicon acea-row row-center-wrapper' :hidden='loading==false'>
				<text class='loading iconfont icon-jiazai'></text>
			</view>
			<!-- <view class="mores-txt" v-if="goodScroll">
				<text>我是有底线的</text>
			</view> -->
		</view>
	</view>
</template>

<script>
	import store from '../store';
	import {
		productList,
		productByidsApi
	} from '@/config/api.js';
	import {
		throttle
	} from '@/utils/validate.js'
	import easyLoadimage from '@/components/easy-loadimage.vue';
	export default {
		name: 'productGrid',
		props: {
			dataConfig: {
				type: Object,
				default: () => {}
			},
		},
		components: {
			easyLoadimage
		},
		data() {
			const that = this
			return {
				tempArr: [],
				numConfig: this.dataConfig.numConfig.val, //展示多少条
				itemStyle: this.dataConfig.itemStyle.tabVal, //商品列表展示方式  单列 两列 三列
				type: this.dataConfig.tabConfig.tabVal || 0, //商品类型 0指定商品，1指定品牌，2指定分类，3指定商户
				selectId: this.dataConfig.selectConfig ? this.dataConfig.selectConfig.activeValue : [], //分类
				brandId: this.dataConfig.activeValueBrand ? this.dataConfig.activeValueBrand.activeValue : [], //品牌
				productIds: this.dataConfig.goodsList.ids || [],
				merId: this.dataConfig.activeValueMer ? this.dataConfig.activeValueMer.activeValue : [], //商户
				params: { //精品推荐分页
					page: 1,
					limit: 10,
					cid: '',
					brandId: '',
					merId: '',
					priceOrder: '',
					salesOrder: ''
				},
				goodScroll: false,
				loading: false,
				scrollFn: throttle(function() {
					// 加载img时才执行滚动监听判断是否可加载
					if (!that.dataConfig.id) return;
					const id = that.dataConfig.id
					const query = uni.createSelectorQuery().in(that);
					query.select('#' + id).boundingClientRect(data => {
						if (!data) return that.scrollFn();
						store.commit('updata_gridTitleList', {
							id,
							pData: data
						})
					}).exec()
				}, 200)
			};
		},
		computed: {
			//商品名称颜色
			titleColor() {
				return {
					'color': this.dataConfig.titleColor.color[0].item,
				}
			},
			//最外层盒子的样式
			boxStyle() {
				return {
					margin: this.dataConfig.mbConfig.val * 2 + 'rpx' + ' ' + this.dataConfig.lrConfig.val * 2 + 'rpx' +
						' ' + 0,
					padding: this.dataConfig.upConfig.val * 2 + 'rpx' + ' ' + 0 + ' ' + this.dataConfig.downConfig.val *
						2 + 'rpx'
				}
			},
			//图片展示样式
			gridGap() {
				return {
					'grid-gap': this.dataConfig.contentConfig.val * 2 + 'rpx'
				}
			},
			//文章图片的圆角和高度
			imgStyle() {
				return {
					'border-radius': this.dataConfig.contentStyle.val * 2 + 'rpx',
				}
			},
			//价格颜色
			priceColor() {
				return {
					'color': this.dataConfig.priceColor.color[0].item,
				}
			},
			//排序，0综合，1销量，2价格
			goodsSort() {
				return this.dataConfig.goodsSort.tabVal
			},
			//内容圆角
			contentStyle() {
				return {
					'border-radius': this.dataConfig.contentStyle.val ? this.dataConfig.contentStyle.val + 'px' : '0',
				};
			},
			// 背景圆角
			contentBgColor() {
				return {
					'background': `linear-gradient(${this.dataConfig.bgColor.color[0].item}, ${this.dataConfig.bgColor.color[1].item})`,
					'borderRadius': this.dataConfig.bgStyle.val * 2 + 'rpx',
				}
			}
		},
		mounted() {
			this.params.page = 1;
			this.goodScroll = false;
			this.tempArr = [];
			//类型为0时，直接加载选中的商品，不为0时根据条件加载商品列表
			if (this.type > 0) {
				this.productslist();
			} else {
				this.getProList();
			}
			this.onScroll()
		},
		//uniapp小程序用deep重写组件样式不生效
		options: {
			styleIsolation: 'shared'
		},
		methods: {
			onScroll() {
				this.scrollFn();
			},
			//根据商品id集合查询对应商品
			getProductByids(data) {
				uni.showLoading({
					title: '加载中...'
				});
				let ids = data.map((item) => item.id).join(',');
				productByidsApi(ids).then((res) => {
						this.tempArr = res.data;
						uni.hideLoading();
					})
					.catch(res => {
						uni.hideLoading();
					});
			},
			getProList() {
				this.getProductByids(this.dataConfig.goodsList.list);
			},
			productslist() {
				if (this.goodScroll) return;
				this.loading = true
				this.params.limit = this.numConfig;
				this.params.cid = this.selectId.join(',');
				this.params.brandId = this.brandId.join(',');
				this.params.merId = this.merId.join(',');
				if (this.goodsSort === 0) {
					this.params.priceOrder = '';
					this.params.salesOrder = '';
				} else if (this.goodsSort === 1) {
					this.params.priceOrder = '';
					this.params.salesOrder = 'desc';
				} else {
					this.params.priceOrder = 'desc';
					this.params.salesOrder = '';
				}

				productList(this.params).then(res => {
					this.$set(this.params, 'page', this.params.page + 1);
					this.goodScroll = this.params.page > res.data.totalPage;
					this.tempArr = this.tempArr.concat(res.data.list || []);
					this.loading = false
				});
			},
			goDetail(item) {
				// this.$emit('detail', item);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.mores-txt {
		text-align: center;
	}

	.text-add {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.listB {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		grid-template-rows: auto;
		width: 100%;

		.item {
			overflow: hidden;

			.pictrue {
				width: 100%;
				height: 224rpx;
				position: relative;

				image {
					width: 100%;
					height: 100%;
				}
			}


			.price {
				margin-top: 4px;
				display: flex;
			}
		}
	}

	.listC {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		grid-template-rows: auto;
		width: 100%;
		overflow: hidden;

		/deep/.origin-img,
		/deep/.easy-loadimage {
			border-bottom-left-radius: 0 !important;
			border-bottom-right-radius: 0 !important;
		}

		.item {
			background-color: #fff;
			overflow: hidden;

			.pictrue {
				width: 100%;
				height: 345rpx;
				overflow: hidden;
				position: relative;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.text-info {
		padding: 16rpx 4rpx;

		.title {
			width: 100%;
			line-height: 40rpx;
			color: #1a1a1a;
			font-size: 14px;
		}

		.price {
			font-size: 28rpx;
			font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: space-between;

			text {
				padding-bottom: 4rpx;
				font-size: 20rpx;
			}

			.car-icon-box {
				width: 22px;
				height: 22px;
				border-radius: 50%;
				background-color: #eeeeee;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 4px;

				image {
					width: 12px;
					height: 12px;
				}
			}
		}
	}

	.mer_badge {
		padding: 0 4rpx;
		background-color: theme;
		color: #fff;
		font-size: 20rpx;
		display: inline-block;
		border-radius: 4rpx;
		line-height: 28rpx;
		height: 28rpx;
	}
	
	.grid-title-box {
	    display: flex;
	    align-items: center;
	    justify-content: center;
	    margin-bottom: 14px;
	    image {
	      width: 10px;
	      height: 11px;
	    }
	    .title-text {
	      font-family: PingFang-SC, PingFang-SC;
	      font-weight: 800;
	      font-size: 16px;
	      color: #000000;
	      margin: 0 8px;
	    }
	  }
</style>